import axios from 'axios';
import qs from 'qs';//转换请求参数格式，需要时使用
import Router from '../router';

axios.defaults.baseURL = 'http://10.5.4.119:8081/'; //部署上线的地址
// axios.defaults.baseURL = 'https://131493m5y4.vicp.fun/'; //图谱开发使用

axios.defaults.timeout = 1200000;
// axios.defaults.timeout = 2000;

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    let token = localStorage.getItem("token");
    if (token) {
        config.headers['Ac-Token'] = token
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (res) {
    console.log("res",res.data)
    if (!!res.data.code && res.data.code != 200) {
        if (res.data.code === 401) {
            localStorage.clear()
            Router.push("/login") //跳转到登陆页面
        }
    }
    // 在接收响应做些什么，例如跳转到登录页
    return res;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});
//post 请求上传header
export function postHead(url, params) {
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: url,
            headers: {
                'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            data: qs.stringify(params)
        }).then(response => {
            resolve(response.data);
        }, err => {
            reject(err);
        })
            .catch((error) => {
                reject(error)
            });
    });
}
//返回一个Promise(发送post请求)
export function postForm(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(params), {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(response => {
            resolve(response.data);
        }, err => {
            reject(err);
        })
            .catch((error) => {
                reject(error)
            });
    });
}
//返回一个Promise(发送post请求)
export function postLogin(url, params) {
    return new Promise((resolve, reject) => {
        axios({
            method: "post",
            url: url,
            headers: {
                "Content-type": "application/x-www-form-urlencoded"
            },
            params: params
        }).then(response => {
            resolve(response.data);
        }, err => {
            reject(err);
        })
            .catch((error) => {
                reject(error)
            });
    });
}
//返回一个Promise(发送post请求)
export function postJson(url, data) {
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: url,
            headers: {
                'Content-type': 'application/json'
            },
            data: data
        }).then(response => {
            resolve(response.data);
        }, err => {
            reject(err);
        })
            .catch((error) => {
                reject(error)
            });
    });
}
//post 请求上传header
export function postData(url, data) {
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: url,
            headers: {
                'Content-type': 'multipart/form-data',
                'Authorization': localStorage.getItem('token')
            },
            data: data
        }).then(response => {
            resolve(response.data);
        }, err => {
            reject(err);
        })
            .catch((error) => {
                reject(error)
            });
    });
}
//返回一个Promise(发送get请求)
export function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: param,
            headers: { 'authorization': localStorage.getItem('token') }
        }).then(response => {
            resolve(response.data)
        }, err => {
            reject(err)
        })
            .catch((error) => {
                reject(error)
            })
    })
}
export default {
    fetchGet,
    postForm,
    postLogin,
    postHead,
    postData,
    postJson
}